package v1

import (
	"gin-wave-im/model/request"
	"gin-wave-im/model/response"
	"gin-wave-im/service"
	"gin-wave-im/util"
	"github.com/gin-gonic/gin"
)

// @Tags Contract
// @Summary 添加好友
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.AddFriend true "对方id"
// @Success 200 {string} string "{"status_code":200,"data":null,"message":"添加好友成功！"}"
// @Router /v1/contact/addFriend [post]
func AddFriend(ctx *gin.Context) {
	dst := &request.AddFriend{}
	if err := ctx.ShouldBindJSON(dst);err != nil {
		response.Fail(ctx, -1, util.Error(err))
		return
	}
	userId := service.GetUserID(ctx)
	err := service.AddFriend(userId, dst.DstId)
	if err != nil {
		response.Fail(ctx, -1, err.Error())
	} else {
		response.Success(ctx, nil, "添加好友成功！")
	}
}

// @Tags Contract
// @Summary 好友列表
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Success 200 {string} string "{"status_code":200,"data":[],"message":"获取好友列表成功！"}"
// @Router /v1/contact/friendsList [get]
func FriendsList(ctx *gin.Context) {
	userId := service.GetUserID(ctx)
	dstIds,err := service.FriendList(userId)
	if err != nil {
		response.Fail(ctx, -1, err.Error())
		return
	}
	dstUsers,err := service.UserList(dstIds)

	response.Success(ctx, dstUsers, "获取好友列表成功！")
}

// @Tags Contract
// @Summary 是否好友
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param DstId query int true "用户id" length(20)
// @Success 200 {string} string "{"status_code":200,"data":[],"message":"是否好友！"}"
// @Router /v1/contact/isFriend [get]
func IsFriend(ctx *gin.Context) {
	dst := &request.AddFriend{}
	if err := ctx.ShouldBindQuery(dst);err != nil {
		response.Fail(ctx, -1, util.Error(err))
		return
	}
	userId := service.GetUserID(ctx)
	rep := response.IsFriendResponse{
		IsFriend: service.IsFriend(userId, dst.DstId),
	}

	response.Success(ctx, rep, "是否好友！")
}